package com.abc.robot01.activity

import android.os.Bundle
import android.util.Log
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.BasicText
import androidx.compose.material3.Card
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import com.abc.robot01.Base64QrCodeImage
import com.abc.robot01.R
import com.abc.robot01.vivemodel.ZhifuViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * 支付
 */
class ZhifuActivity : BaseVoiceActivity() {

    private val viewModel: ZhifuViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val designPackageId = intent.getStringExtra("designPackageId")!!
        viewModel.designPackageId.value = designPackageId;
        val housePlansId = intent.getStringExtra("housePlansId")!!
        viewModel.housePlansId.value = housePlansId;

        val housePlanDataId = intent.getStringExtra("housePlansDataId")!!
        viewModel.housePlanDataId.value = housePlanDataId;

        val userName = intent.getStringExtra("userName")!!
        viewModel.userName.value = userName;

        val customerPhone = intent.getStringExtra("customerPhone")!!
        viewModel.customerPhone.value = customerPhone;

        val skyBoxId = intent.getStringExtra("skyBoxId")!!
        viewModel.skyBoxId.value = skyBoxId;

        val fee = intent.getStringExtra("fee")!!
        viewModel.fee.value = fee

        viewModel.name.value = intent.getStringExtra("name")!!
        viewModel.paymentMethod.value = intent.getIntExtra("paymentMethod",-1)
        val renderRoomIds = intent.getStringExtra("renderRoomIds")!!.split(",")
        viewModel.renderRoomIds.addAll(renderRoomIds)
        viewModel.startPay()
        setContent {
            val scrollState = rememberScrollState()
            MaterialTheme {
                Box {
                    Image(painter = painterResource(id = R.drawable.z1), contentDescription = "")
                    Row(modifier = Modifier.padding(30.px)) {
                        BackButton {
                        }
                    }
                    Content()
                }
            }
        }
        Log.e(TAG, "onCreate");
    }

    @Composable
    fun BoxScope.Content() {

        val coroutineScope = rememberCoroutineScope()

        val selected = remember {
            mutableStateOf("渲染钱包支付")
        }

//        val fail = remember {
//            viewModel.fail
//        }

        val show = remember {
            viewModel.show
        }

        val paySuccess by remember {
            viewModel.paySuccess
        }

//        LaunchedEffect(paySuccess) {
//            if (paySuccess.value) {
//
//            }
//        }

        val amount by remember {
            viewModel.fee
        }

        Card(
            modifier = Modifier
                .width(800.px)
                .height(748.px)
                .align(Alignment.Center)
                .background(Color.Transparent),
            colors = androidx.compose.material3.CardDefaults.cardColors(
                containerColor = Color.White // 设置背景色为红色
            ),
        ) {
            Row(modifier = Modifier.padding(30.px)) {
                BasicText(
                    modifier = Modifier.clickable(
                        indication = null,  // Removes the ripple effect
                        interactionSource = remember { MutableInteractionSource() }
                    ) {
//                        selected.value = "渲染钱包支付"
                    },
                    text = if(viewModel.paymentMethod.value ==1) "渲染钱包支付" else "微信支付" ,
                    style = TextStyle(
                        fontSize = 30.mysp,
                        fontWeight = FontWeight.Bold,
                        color =  MYBLACK
                    ),
                )
//                Spacer(modifier = Modifier.width(30.px))
//                BasicText(
//                    modifier = Modifier.clickable(indication = null,  // Removes the ripple effect
//                        interactionSource = remember { MutableInteractionSource() }) {
//                        selected.value = "微信支付"
//                    },
//                    text = "微信支付",
//                    style = TextStyle(
//                        fontWeight = FontWeight.Bold,
//                        fontSize = 30.mysp,
//                        color = if (selected.value == "微信支付") MYBLACK else MYBLACK_Percent60
//                    ),
//                )
            }

            Column(
                horizontalAlignment = Alignment.CenterHorizontally,
                modifier = Modifier
                    .fillMaxWidth()
                    .offset(y = 50.px)
            ) {

                if (show.value) {
                    Row(verticalAlignment = Alignment.Bottom) {
                        BasicText(
                            text = "应付金额",
                            style = TextStyle(
                                fontSize = MyFont_Size,
                                color = Color.Black,
                                textAlign = TextAlign.Center,
                            ),
                        )

                        Spacer(modifier = Modifier.width(20.px))

                        BasicText(
                            text = "¥",
                            style = TextStyle(
                                fontSize = 20.mysp,
                                color = MYBLUE,
                                textAlign = TextAlign.Center,
                                fontWeight = FontWeight.Bold
                            ),
                        )

                        BasicText(
                            text = amount,
                            style = TextStyle(
                                fontSize = 38.mysp,
                                color = MYBLUE,
                                textAlign = TextAlign.Center,
                                fontWeight = FontWeight.Bold
                            ),
                        )


                        BasicText(
                            text = "元",
                            style = TextStyle(
                                fontSize = 20.mysp,
                                color = MYBLUE,
                                textAlign = TextAlign.Center,
                            ),
                        )
                    }
                }
                Spacer(modifier = Modifier.height(37.px))

                if (show.value) {
                    Base64QrCodeImage(viewModel.base64)
                    Spacer(modifier = Modifier.height(26.px))
                    BasicText(
                        text = "请使用“微信”扫码完成授权",
                        style = TextStyle(
                            fontSize = 24.mysp,
                            color = MYBLACK_Percent60,
                            textAlign = TextAlign.Center,
                        ),
                    )
                } else {
                        Image(
                            painter = painterResource(id =  if(paySuccess) R.drawable.cg else R.drawable.fail),
                            contentDescription = "",
                            modifier = Modifier.size(350.px, 350.px)
                        )
                        BasicText(
                            text = if(paySuccess)  "支付成功" else "支付失败",
                            style = TextStyle(
                                fontSize = 30.mysp,
                                color =  if(paySuccess) MYBLUE else MYRED,
                                textAlign = TextAlign.Center,
                            ),
                        )
                    if(paySuccess) {
                        LaunchedEffect(Unit) {
                            coroutineScope.launch {
                                delay(2000) // Wait for 2 seconds
                                Log.e(TAG, "支付成功,打开等待界面:startActivity  WaitingActivity")
                                startActivity<WaitingActivity>()
                            }
                        }
                    }
                }
            }
        }
    }

    companion object {
        val TAG = "ZhifuActivity"
    }

}
